package com.haima.ziliao.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haima.common.utils.SecurityUtils;
import com.haima.ziliao.domain.PChanpin;
import com.haima.ziliao.domain.PYanxuetuijian;
import com.haima.ziliao.service.IPChanpinService;
import com.haima.ziliao.service.IPYanxuetuijianService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haima.common.annotation.Log;
import com.haima.common.core.controller.BaseController;
import com.haima.common.core.domain.AjaxResult;
import com.haima.common.enums.BusinessType;
import com.haima.ziliao.domain.PLvxingshe;
import com.haima.ziliao.service.IPLvxingsheService;
import com.haima.common.utils.poi.ExcelUtil;
import com.haima.common.core.page.TableDataInfo;

/**
 * 旅行社Controller
 * 
 * @author hima
 * @date 2024-12-21
 */
@RestController
@RequestMapping("/ziliao/lvxingshe")
public class PLvxingsheController extends BaseController
{
    @Autowired
    private IPLvxingsheService pLvxingsheService;
    @Autowired
    private IPChanpinService chanpinService;
    @Autowired
    private IPYanxuetuijianService yanxuetuijian;

    /**
     * 查询旅行社列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PLvxingshe pLvxingshe)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            pLvxingshe.setUid(SecurityUtils.getUserId());
        }
        List<PLvxingshe> list = pLvxingsheService.selectPLvxingsheList(pLvxingshe);
        for(int i=0;i<list.size();i++){
            PYanxuetuijian tj = yanxuetuijian.getOne(new QueryWrapper<PYanxuetuijian>()
                    .eq("bid",list.get(i).getId())
                    .eq("type","lxs"));
            if(tj==null){
                list.get(i).setTuijian(0L);
            }else{
                list.get(i).setTuijian(tj.getId());
            }

        }
        return getDataTable(list);
    }

    /**
     * 导出旅行社列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:lvxingshe:export')")
    @Log(title = "旅行社", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PLvxingshe pLvxingshe)
    {
        List<PLvxingshe> list = pLvxingsheService.selectPLvxingsheList(pLvxingshe);
        ExcelUtil<PLvxingshe> util = new ExcelUtil<PLvxingshe>(PLvxingshe.class);
        util.exportExcel(response, list, "旅行社数据");
    }

    /**
     * 获取旅行社详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        PLvxingshe lvxingshe = pLvxingsheService.selectPLvxingsheById(id);
        Long num = chanpinService.count(new QueryWrapper<PChanpin>().eq("lid",lvxingshe.getId()));
        lvxingshe.setCpnum(num);
        return success(lvxingshe);
    }

    /**
     * 新增旅行社
     */
    @PreAuthorize("@ss.hasPermi('ziliao:lvxingshe:add')")
    @Log(title = "旅行社", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PLvxingshe pLvxingshe)
    {
        pLvxingshe.setUid(SecurityUtils.getUserId());
        return toAjax(pLvxingsheService.insertPLvxingshe(pLvxingshe));
    }

    /**
     * 修改旅行社
     */
    @PreAuthorize("@ss.hasPermi('ziliao:lvxingshe:edit')")
    @Log(title = "旅行社", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PLvxingshe pLvxingshe)
    {
        return toAjax(pLvxingsheService.updatePLvxingshe(pLvxingshe));
    }

    /**
     * 删除旅行社
     */
    @PreAuthorize("@ss.hasPermi('ziliao:lvxingshe:remove')")
    @Log(title = "旅行社", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pLvxingsheService.deletePLvxingsheByIds(ids));
    }
}
